package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.DepartmentMapper;
import com.hqyj.pojo.Department;
import com.hqyj.pojo.Income;
import com.hqyj.dao.IncomeMapper;
import com.hqyj.pojo.Vids;
import com.hqyj.service.IDepartmentService;
import com.hqyj.service.IIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 收入表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class IncomeServiceImpl implements IIncomeService {

    @Autowired
    IncomeMapper incomeMapper;
    @Override
    public HashMap<String,Object> selectList(Income income) {
        //创建分页对象
        Page<Income> page = new Page<>(income.getPage(),income.getRow());
        HashMap<String,Object> map = new HashMap<>();
        //获取前端的日期
        Date date = income.getIncomeTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String day = simpleDateFormat.format(date);
        //定义查询对象，查询本日的收入信息
        QueryWrapper<Income> wrapper = new QueryWrapper<>();
        wrapper.like(day!=null&&!day.equals(""),"income_time",day);
        wrapper.eq(income.getIncomeId()!=null,"income_id",income.getIncomeId());
        wrapper.like(income.getCategoryName()!=null&&!income.getCategoryName().equals(""),"category_name",income.getCategoryName());

        //查询数据库
        Page<Income> incomePage=incomeMapper.selectPage(page,wrapper);

        //当前页集合
        map.put("list",incomePage.getRecords());
        //总页数
        map.put("totalPage",incomePage.getPages());
        //总条数
        map.put("total",incomePage.getTotal());
        //当前页
        map.put("curPage",income.getPage());
        if(income.getPage()<=1){
            //上一页
            if (income.getPage()==0){
                map.put("prePage",0);
            }else {
                map.put("prePage",1);
            }
        }else{
            map.put("prePage",income.getPage()-1);

        }
        if(income.getPage()>=incomePage.getPages()){
            //下一页
            map.put("nextPage",incomePage.getPages());

        }else{
            map.put("nextPage",income.getPage()+1);

        }
        //每页显示条数
        map.put("row",income.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> update(Income income) {
        HashMap<String,Object> map = new HashMap<>();
        int i = incomeMapper.updateById(income);
        if (i>0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Income income) {
        HashMap<String,Object> map = new HashMap<>();
        int i = incomeMapper.deleteById(income);
        if (i>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }
}
